from loguru import logger
import requests
import json
from service.utils import config


# 老版本
def request_uuyp_old_detail(options={}, proxies=None):
    try:
        url = "https://api.youpin898.com/api/homepage/es/commodity/GetCsGoPagedList"
        payload = json.dumps(
            {
                # "templateId": options.get('templateId', None),
                # "keyWords": options.get('keyWords', None),
                "pageSize": 1,
                "pageIndex": 1,
                "sortType": 1,
                "listSortType": 1,
                "listType": 10,
                # "userId": 522009,
                "stickersIsSort": False,
                "stickers": {},
                **options,
            }
        )
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36 Edg/137.0.0.0",
            "Content-Type": "application/json",
            "Authorization": f'Bearer {config.get("UUYP", "TOKEN")}',
            "Accept": "*/*",
            "Host": "api.youpin898.com",
            "Connection": "keep-alive",
            # 'Cookie': 'acw_tc=0b3c7da217502994338403109eeddfd51cadeb875779cfaf4727b79e313d18'
        }
        response = requests.request(
            "POST", url, headers=headers, data=payload, timeout=10
        )
        result = response.json()
        if (
            result.get("Code", 400) != 0
            or len(result.get("Data", {}).get("CommodityList", [])) == 0
        ):
            raise Exception("空数据或请求失败！")
        return result
    except Exception as e:
        logger.warning(f"{options.get('item_name', '未知')} UUYP查询失败: [{e}]")
        return {}


# 新版本
def request_uuyp_new_detail(params={}, proxies=None):
    try:
        url = "https://api.youpin898.com/api/homepage/pc/goods/market/queryOnSaleCommodityList"
        payload = json.dumps(
            {
                "listSortType": 0,
                "sortType": 0,
                "filterMap": {"Type": ["CSGO_Type_SMG_Unlimited"]},
                "pageSize": 20,
                "pageIndex": 1,
            }
        )
        headers = {
            "Apptype": "1",
            "appType": "1",
            "App-Version": "5.26.0",
            "Appversion": "5.26.0",
            "Content-Type": "application/json",
            "platform": "pc",
            "Host": "api.youpin898.com",
            "Origin": "https://www.youpin898.com",
            "Referer": "https://www.youpin898.com/",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36 Edg/141.0.0.0",
            "Authorization": config.get("UUYP", "TOKEN"),
            "deviceId": config.get("UUYP", "DEVICEID"),
            "deviceUk": config.get("UUYP", "DEVICEUK"),
            "uk": config.get("UUYP", "UK"),
            # "Accept": "*/*",
            # "Connection": "keep-alive",
            # "Cookie": "acw_tc=1a1c721217602334638001824ef894e4f940db5249a415043ac7013d1ad526",
        }
        response = requests.post(url, headers=headers, data=payload)
        result = response.json()
        if result.get("Code", 400) != 0 or len(result.get("Data", [])) == 0:
            raise Exception("空数据或请求失败！")
        return result
    except Exception as e:
        logger.warning(f"{params.get('item_name', '未知')} UUYP查询失败: [{e}]")
        return {}
